import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import com.jgoodies.forms.factories.DefaultComponentFactory;

/* 
 * NotesGUI.java - This class is the graphical represantion of Notes.java.
 * 
 * @Author : BilCIS
 */


public class NotesGUI extends JPanel
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -921838331112413324L;

	// Variables declaration
	private int counter;
	
	private JPanel contentPane;
	
	private JButton removeButton;
	private JButton addButton;
	private JButton upButton;
	private JButton downButton;
	
	private	JLabel lblTopic;
	private	JLabel lblTopics;
	private	JLabel lblNotes;
	
	private	TextArea topics;
	
	private TextField topicTF; 
	private JTextArea noteTF; 
	
	private Notes notes;
	private Employee emp;
	// End of variables declaration

	public NotesGUI(Employee emp) throws SQLException
	{
		this.emp = emp;
		notes = new Notes(emp);
		counter = notes.getNumOfNotes() - 1;
		create();
		this.setVisible(true);
	}
	
	private void create()
	{
		//
		//Create controls
		//
		contentPane = new JPanel(null);
		removeButton = new JButton("Remove");
		addButton = new JButton("Add");
		upButton = new JButton("");
		downButton = new JButton("");
		topicTF = new TextField(notes.getFirstTopic());
		noteTF = new JTextArea(notes.getFirstNote());
		lblTopic = DefaultComponentFactory.getInstance().createLabel("Topic:");
		lblTopics = DefaultComponentFactory.getInstance().createLabel("Other Notes:");
		lblNotes = DefaultComponentFactory.getInstance().createLabel("Note:");
		topics = new TextArea("");
		//	
		//Register action listeners
		//
		removeButton.addActionListener( new removeButton_Action() );
		addButton.addActionListener( new addButton_Action() );
		upButton.addActionListener( new upButton_Action() );
		downButton.addActionListener( new downButton_Action() );
		noteTF.addFocusListener(new FocusListener() {
			public void focusGained (FocusEvent e) {
				noteTF.setText("");
			}

			@Override
			public void focusLost(FocusEvent arg0) {
				
			}
		});
		topicTF.addFocusListener(new FocusListener() {
			public void focusGained (FocusEvent e) {
				topicTF.setText("");
			}

			@Override
			public void focusLost(FocusEvent arg0) {
				
			}
		});
		//	
		// Mnemonics
		//
		addButton.setMnemonic('a');
		removeButton.setMnemonic('r');
		upButton.setMnemonic(KeyEvent.VK_UP);
		downButton.setMnemonic(KeyEvent.VK_DOWN);
		//
		//Add controls to pane
		//
		this.add(contentPane);
		contentPane.add(removeButton);
		contentPane.add(addButton);
		contentPane.add(upButton);
		contentPane.add(downButton);
		contentPane.add(topicTF);
		contentPane.add(noteTF);
		contentPane.add(lblTopic);
		contentPane.add(lblTopics);
		contentPane.add(lblNotes);
		contentPane.add(topics);
		//	
		//Set bounds
		//	
		contentPane.setBounds(0,0,430,350);
		removeButton.setBounds(175,270,90,40);
		addButton.setBounds(75,270,90,40);
		upButton.setBounds(260,40,40,40);
		downButton.setBounds(260,90,40,40);
		topicTF.setBounds(60,10,190,40);
		noteTF.setBounds(60,60,190,200);
		lblTopic.setBounds(0,10,50,40);
		lblTopics.setBounds(310,0,120,20);
		lblNotes.setBounds(0,60,50,20);
		topics.setBounds(310,25,110,310);
		//	
		//	Setting Icons
		//
		upButton.setIcon(new ImageIcon( new File("up.png").getAbsolutePath() ));
		downButton.setIcon(new ImageIcon( new File("down.png").getAbsolutePath() ));
		//
		//	Topic label
		//
		lblTopic.setForeground(new Color(0, 100, 0));
		lblTopic.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		//
		//	Topics label
		//
		lblTopics.setForeground(new Color(255, 127, 40));
		lblTopics.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		//
		//	Notes label
		//
		lblNotes.setForeground(new Color(0, 100, 0));
		lblNotes.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		//
		//	Topics
		//
		topics.setEditable(false);
		topics.setForeground(new Color(0, 0, 255));
		topics.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
		topics.setText(notes.getTopics());
		//********************** Colors *********************//
		contentPane.setBackground(emp.getColor());
		
		//
		// Settings
		//	
		this.setSize(new Dimension(430, 350));
		this.setLayout(null);
	}
	
	// removes the note
	public class removeButton_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			notes.removeNote( counter );
			
			counter--;
			topicTF.setText( notes.getTopic( counter ) );
			noteTF.setText( notes.getNote( counter ) );
			topics.setText(notes.getTopics());
		}
	}
	
	// adds the note
	public class addButton_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			notes.addNote(topicTF.getText(), noteTF.getText());

			counter++;
			topics.setText(notes.getTopics());
		}
	}
	
	// swithches to up note
	public class upButton_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			if ( !(notes.getNumOfNotes() -1 == counter) )
				counter++;
			topicTF.setText( notes.getTopic( counter ) );
			noteTF.setText( notes.getNote( counter ) );
		}
	}
	
	// swithches to down note
	public class downButton_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			if ( !(counter == 0) )
				counter--;
			topicTF.setText( notes.getTopic( counter ) );
			noteTF.setText( notes.getNote( counter ) );
		}
	}
}
